#!python
# SCons based build file for the property template crap
# Base file
import SCons.Environment;   # Get the environment crap
import SCons;
import sys, os, re
import SConsAddons.Options.Boost
import SConsAddons.Util

pj = os.path.join

try:
   import wing.wingdbstub;       # stuff for debugging
except:
   print '[WRN] Unable to load wing debugging extensions'


# ##################################################
# Helper methods
# ##################################################
GetPlatform = SConsAddons.Util.GetPlatform

def BuildLinuxEnvironment():
   "Builds a base environment for other modules to build on set up for linux"
   global optimize, profile

   CXX = os.path.basename(WhereIs('g++3') or 'g++')
   LINK = CXX
   CXXFLAGS = ['-Wall', '-fexceptions']
   LINKFLAGS = []

   # Enable profiling?
   if profile != 'no':
      CXXFLAGS.extend(['-pg'])
      LINKFLAGS.extend(['-pg'])

   # Debug or optimize build?
   if optimize:
      CXXFLAGS.extend(['-DNDEBUG', '-O2'])
   else:
      CXXFLAGS.extend(['-D_DEBUG', '-g'])

   return Environment(
      ENV         = os.environ,
      CXX         = CXX,
      CXXFLAGS    = CXXFLAGS,
      LINK        = LINK,
      LINKFLAGS   = LINKFLAGS
   )

def BuildIRIXEnvironment():
   "Builds a base environment for other modules to build on set up for IRIX"
   global optimize, profile

   CXX = 'CC'
   LINK = CXX
   CXXFLAGS = ['-n32', '-mips3', '-LANG:std', '-w2']
   LINKFLAGS = CXXFLAGS

   # Enable profiling?
   if profile != 'no':
      CXXFLAGS.extend([])
      LINKFLAGS.extend([])

   # Debug or optimize build?
   if optimize:
      CXXFLAGS.extend(['-DNDEBUG', '-O2'])
   else:
      CXXFLAGS.extend(['-D_DEBUG', '-g', '-gslim'])

   return Environment(
      ENV         = os.environ,
      CXX         = CXX,
      CXXFLAGS    = CXXFLAGS,
      LINK        = LINK,
      LINKFLAGS   = LINKFLAGS
   )

def BuildWin32Environment():
   return Environment(ENV = os.environ)

# ###############################
# Create builder
# ###############################
help_text = "\n---- The Build System ----\n";

# Get command-line arguments
optimize = ARGUMENTS.get('optimize', 'no') != 'no'
profile = ARGUMENTS.get('profile', 'no')
   
# Create the initial environment
#env = Environment(
#   ENV         = os.environ,
#
#   CPPPATH     = [],
#   LIBPATH     = [],
#   LIBS        = [],
#);
# Create and export the base environment
if GetPlatform() == 'irix':
   env = BuildIRIXEnvironment()
elif GetPlatform() == 'linux':
   env = BuildLinuxEnvironment()
elif GetPlatform() == 'win32':
   env = BuildWin32Environment()
else:
   print 'Unsupported build environment: ' + GetPlatform()
   sys.exit(-1)

# --- Add options --- #
options_cache_filename = 'options.cache.' + GetPlatform()
opts = SConsAddons.Options.Options(files = [options_cache_filename, 'options.custom'], 
                                   args= ARGUMENTS);
boost_options = SConsAddons.Options.Boost.Boost("boost","1.33.0",
                                                useDebug=not optimize,
                                                useMt = True,
                                                libs = ['python'],
                                                required=True)
opts.AddOption( boost_options )

help_text += opts.GenerateHelpText(env);
help_text += """
You can store configuration options in the file: options.custom
This file will be loaded each time.  Note: Options are cached in the file: options.cache.
"""
if 0:
   print "---- boost settings ---"
   boost_options.dumpSettings()

if not SConsAddons.Util.hasHelpFlag():
   print "Setting up build settings...\n";

   opts.Update(env);
   opts.Save(options_cache_filename, env);


   # Add dynamic linking library
#   if GetPlatform() != 'win32':
#      env.Append(LIBS = ['dl',])
   env.Append(CPPPATH=['.'])         
   py_mod_env = env.Copy()
   py_app_env = env.Copy()
      
   # --- Set up sources --- #
   hello_app_srcs = [];  # List of source files for the ggv_viewer
   hello_app_srcs += Split(""" hello_main.cpp """);
   hello_module_srcs = [];
   hello_module_srcs += Split(""" Hello_module.cpp """);

   print "hello_app_srcs srcs: ", [str(f) for f in hello_app_srcs];
   
   boost_options.updatePythonModEnv(py_mod_env)
   boost_options.updatePythonEmbeddedEnv(py_app_env)
   
   py_mod_env.SharedLibrary('hello', hello_module_srcs)
   py_app_env.Program("helloapp", hello_app_srcs)
   
Default(".");

# ---- List targets ----- #
#help_text += """
#Targets:
#   - tests: Build the test suite (and if I was smarter it would run)
#   - runtests: If I was really smart this would run then after building
#"""

Help(help_text);

# end
